package com.nuanqian.springbootcommon.utils.paging;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResult implements Serializable {


    //总条数
    private Long total;
    //总页数
    private Long totalPage;
    //每页条数 默认10
    private Integer pageSize = 10;
    //当前页面
    private Integer showPage = 1;

    private int start;//针对一对多 实际索引
    private int end;//针对一对多 实际查询条数

    //数据
    private List<?> data;

    private String msg;
    private Integer code;



    public Long getTotalPage() {
        return totalPage = this.total % this.pageSize == 0 ?
                (this.total / this.pageSize) : (this.total/this.pageSize+1);
    }

    public void setOntToManyPage(List<Integer> list,Integer showPage,Integer pageSize){
        int tmpStart = (showPage - 1) * pageSize;
        int tmpEnd = showPage * pageSize;
        int max = list.size();
        start = list.subList(0, tmpStart > max ? max : tmpStart).stream().mapToInt(Integer::intValue).sum();
        end = list.subList(tmpStart, tmpEnd > max ? max : tmpEnd).stream().mapToInt(Integer::intValue).sum();
    }
}
